
import { HTTP } from "../../../utils/http"
import api from '../../../utils/api.js'
import { percentage } from "../../../utils/util"
let http = new HTTP()

Page({
    /**
     * 页面的初始数据
     */
    data: {
        username: "",
        phone: "",
        group: {},
        shop: {},
        product: {},
        count: 1, // 购买数量
        price: 0, // 总金额
        diff_price: 0, // 总差价（成团返）
        progress: 0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function () {
        // 获取拼团信息
        let group_info = wx.getStorageSync('group_info');
        if (group_info) {
            let price = group_info.group.original_price * this.data.count;
            let diff_price = (group_info.group.original_price - group_info.group.final_price) * this.data.count;
            let progress = percentage(group_info.group.joined_count, group_info.group.joiner_count);
            this.setData({
                group: group_info.group,
                shop: group_info.shop,
                product: group_info.product,
                price: price.toFixed(2),
                diff_price: diff_price.toFixed(2),
                progress: progress
            })
        }
        // 获取地址信息
    },

    onShow: function () {
        let address = wx.getStorageSync('integralAddress');
        if (address) {
            this.setData({
                username: address.name || "",
                phone: address.phone || "",
            });
        }
    },

    addCount: function () {
        if (this.data.count >= this.data.group.product_stock) {
            return wx.showToast({
                title: '库存不足',
                icon: "none"
            });
        }
        let modifyData = {
            count: this.data.count + 1,
            price: (this.data.group.original_price * (this.data.count + 1)).toFixed(2),
            diff_price: ((this.data.group.original_price - this.data.group.final_price) * (this.data.count + 1)).toFixed(2)
        }
        this.setData(modifyData)
    },

    incCount: function () {
        if (this.data.count <= 1) {
            return false;
        }
        let modifyData = {
            count: this.data.count - 1,
            price: (this.data.group.original_price * (this.data.count - 1)).toFixed(2),
            diff_price: ((this.data.group.original_price - this.data.group.final_price) * (this.data.count - 1)).toFixed(2)
        }
        this.setData(modifyData)
    },

    createOrder() {
        let group_id = this.data.group.id;
        if (!this.data.username
            || !this.data.phone) {
            return wx.showToast({
                title: '请重新选择收货信息',
                icon: 'none'
            });
        }
        // 下单
        http.request({
            url: api.groupOrderCreate,
            data: {
                gb_id: this.data.group.id,
                username: this.data.username,
                phone: this.data.phone,
                count: this.data.count,
            },
            method: "post",
            success: function (res) {
                // 下单成功，获取到订单号
                let order_id = res.data.order_no;
                // 调用支付接口
                http.request({
                    url: "/group/pay",
                    data: { order_id },
                    method: "post",
                    success: function (res) {
                        // 获取到支付凭证，并调起微信支付弹窗
                        if (res.code == 200) {
                            wx.requestPayment({
                                timeStamp: res.data.timeStamp,
                                nonceStr: res.data.nonceStr,
                                package: res.data.package,
                                signType: 'MD5',
                                paySign: res.data.paySign,
                                // 成功支付
                                success: function () {
                                    // 支付成功，跳转支付完成页面
                                    wx.redirectTo({
                                        url: '/pages/group-buy/pay-success/index',
                                    });
                                },

                                // 支付失败
                                fail: function () {
                                    // 支付失败
                                    wx.showToast({
                                        title: '支付失败',
                                    });

                                    setTimeout(function () {
                                        wx.navigateBack({
                                            delta: 1,
                                        });
                                    }, 500)
                                },
                                // 支付结束
                                complete: function () {
                                    http.request({
                                        url: api.groupPayNotify,
                                        method: "post",
                                        data: { order_id }
                                    });
                                }
                            })
                        }
                    }
                })
            }
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})